//
//  YWAnchorViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWAnchorViewController.h"
#import "YWAnchorListModel.h"
#import "YWAnchorContentTableViewCell.h"

#import "YWMoreAnchorViewController.h"
#import "YWAnchorIntroduceViewController.h"


@interface YWAnchorViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *anchorTableView;

@property (nonatomic, strong) NSMutableArray *anchorArray;

@end

@implementation YWAnchorViewController

- (void)dealloc
{
    _anchorTableView.delegate = nil;
    _anchorTableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    self.anchorArray = [NSMutableArray array];
    [self getData];
}

- (void) getView
{
    self.anchorTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - (64 + 49 + 30)) style:UITableViewStyleGrouped];
    _anchorTableView.delegate = self;
    _anchorTableView.dataSource = self;
    _anchorTableView.backgroundColor = [UIColor clearColor];
    _anchorTableView.sectionFooterHeight = 10;
    _anchorTableView.separatorStyle = 0;
    [self.view addSubview:_anchorTableView];
    
    //注册
    [_anchorTableView registerClass:[YWAnchorContentTableViewCell class] forCellReuseIdentifier:@"anchorContentCell"];
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self getData];
}


#define mark ----------- 代理方法 ------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _anchorArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWAnchorContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anchorContentCell"];
    cell.selectionStyle = UITableViewCellStyleDefault;
    cell.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    YWAnchorListModel *listModel = [_anchorArray objectAtIndex:indexPath.section];
    cell.listModel = listModel;
    cell.uidBlock = ^(NSInteger uidNum){
        YWAnchorIntroduceViewController *aivc = [[YWAnchorIntroduceViewController alloc] init];
        aivc.hidesBottomBarWhenPushed = YES;
        aivc.uid = uidNum;
        [self.navigationController pushViewController:aivc animated:YES];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 * FITHEIGHT;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45 * FITHEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 45 * FITHEIGHT)];
    headerView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 5 * FITHEIGHT)];
    lineLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:lineLabel];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * FITWIDTH, 20 * FITHEIGHT, 13 * FITWIDTH, 13 * FITHEIGHT)];
    arrowImageView.image = [UIImage imageNamed:@"三角"];
    [headerView addSubview:arrowImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25 * FITWIDTH, 17 * FITHEIGHT, 100 * FITWIDTH, 20 * FITHEIGHT)];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    label.text = [[_anchorArray objectAtIndex:section] title];
    [headerView addSubview:label];
    
    UIImageView *moreImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * FITWIDTH, 20 * FITHEIGHT, 40 * FITWIDTH, 15 * FITHEIGHT)];
    moreImageView.image = [UIImage imageNamed:@"liveRadioSectionMore_Normal"];
    moreImageView.tag = 1000 + section;
    moreImageView.userInteractionEnabled = YES;
    [headerView addSubview:moreImageView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMoreImageView:)];
    [moreImageView addGestureRecognizer:tap];
    
    return headerView;
}

- (void)didTapMoreImageView:(UITapGestureRecognizer *)tap
{
    NSInteger num = tap.view.tag - 1000;
    YWMoreAnchorViewController *avc = [[YWMoreAnchorViewController alloc] init];
    avc.hidesBottomBarWhenPushed = YES;
    avc.nameString = [[_anchorArray objectAtIndex:num] name];
    avc.titleString = [[_anchorArray objectAtIndex:num] title];
    [self.navigationController pushViewController:avc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (void) getData
{
    NSString *string = @"http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1&statEvent=pageview%2Ftab%40发现_主播";
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *listArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in listArray) {
            YWAnchorListModel *listModel = [[YWAnchorListModel alloc] initWithDic:dic];
            [_anchorArray addObject:listModel];
        }
        [_anchorTableView reloadData];
        [_anchorTableView.mj_header endRefreshing];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
